package com.ruoyi.web.xxj.contoller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.xxj.domain.pojo.PrePayReq;
import com.ruoyi.web.xxj.service.IWxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 收入管理Controller
 *
 * @author zhurenwang
 * @date 2023-07-26 14:25:04
 */

@Api(tags = "微信支付相关接口")
@Slf4j
@RestController
@RequestMapping("/xxj/wxPay")
public class WXPayController extends BaseController {

    @Autowired
    private IWxPayService wxPayService;

    /**
     * 获取预支付参数
     */
    @PostMapping("/prePay")
    @ApiOperation("获取预支付参数")
    public AjaxResult prePay(@Valid @RequestBody PrePayReq prePayReq) {
        try {
            return wxPayService.prePay(prePayReq);
        } catch (Exception e) {
            log.error("生成预支付订单失败", e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 根据已有订单获取预支付参数
     */
    @GetMapping("/prePayByOrder")
    @ApiOperation("根据已有订单获取预支付参数")
    public AjaxResult prePayByOrder(@ApiParam("订单号") String orderNo, @ApiParam("订单类型（class|product）") String type) {
        try {
            return wxPayService.prePayByOrder(orderNo, type);
        } catch (Exception e) {
            log.error("根据已有订单获取预支付参数", e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 微信支付回调
     */
    @PostMapping("/notify")
    @ApiOperation("微信支付回调")
    public ResponseEntity<Void> notify(HttpServletRequest request, @RequestBody String body) {
        return wxPayService.notify(request, body);
    }

    /**
     * 微信退款回调
     */
    @PostMapping("/refundNotify")
    @ApiOperation("微信退款回调")
    public ResponseEntity<Void> refundNotify(HttpServletRequest request, @RequestBody String body) {
        return wxPayService.refundNotify(request, body);
    }

}
